package com.yootk.action;

import com.yootk.common.action.abs.AbstractBaseAction;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 全局数据绑定
 */
@RestController
@RequestMapping("/data/*")
public class GlobalDataAction extends AbstractBaseAction {

    @RequestMapping("echo")
    public Object echo(String message, Model model) {
        // 获取绑定的Map集合
        Map<String, Object> map = (Map<String, Object>) model.asMap().get("bindModel");
        map.put("data", "【ECHO】" + message);
        return map;
    }

    // 直接注入绑定数据
    @RequestMapping("echo2")
    public Object echo2(String message, @ModelAttribute("bindModel") Map<String, Object> map) {
        map.put("data", "【ECHO】" + message);
        return map;
    }

    @RequestMapping("calc")
    public Object calc(int x, int y, Model model) {
        // 获取绑定的Map集合
        Map<String, Object> map = (Map<String, Object>) model.asMap().get("bindModel");
        map.put("data", x / y);
        return map;
    }
}
